<template>
  <div class="search-box u-flex">
    <van-icon
      class="u-flex-1"
      name="arrow-left"
      size="20"
      color="#ffffff"
      @click="handleToBack"
    />
    <van-search
      class="u-flex-8"
      v-model="keyword"
      background="rgb(16, 25, 37)"
      shape="round"
      autofocus
      placeholder="搜索玩家昵称、比赛Id"
      @search="handleToSearch"
    />
  </div>
  <div class="search-placeholder"></div>
  <!-- 历史搜索 -->
  <div v-if="searchStatus === 'init'">
    <template v-if="historyList.length > 0">
      <div class="u-p-10 u-flex u-row-between">
        <span class="u-font-14 u-type-info">历史搜索</span>
        <van-icon
          name="delete-o"
          size="20"
          color="#909399"
          @click="handleToClearHistory"
        />
      </div>
      <div class="u-p-r-10 u-p-l-10 u-flex u-flex-wrap">
        <van-tag
          @click="handleToSearchHistory(item)"
          class="u-m-r-4 u-m-b-4"
          round
          :type="Number.isNaN(Number(item)) ? 'success' : 'primary'"
          size="medium"
          v-for="(item, index) in historyList"
          :key="index"
          >{{ item }}</van-tag
        >
      </div>
    </template>
  </div>
  <!-- 搜索结果 -->
  <div class="search-list" v-else>
    <div class="loading-box" v-if="searchStatus === 'search'">
      <van-loading size="24px" vertical>加载中...</van-loading>
    </div>
    <van-empty
      v-else-if="searchStatus === 'error'"
      class="custom-image"
      image="https://iconfont.alicdn.com/t/ccf5c84b-f674-418c-b248-dd9871bfcc9e.png"
      image-size="200"
      description="加载失败"
    >
    </van-empty>
    <template v-else>
      <template v-if="searchList.length > 0">
        <van-cell
          is-link
          v-for="item in searchList"
          :key="item.account_id"
          class="u-flex"
          @click="handleToHome(item.account_id)"
        >
          <template #title>
            <div class="u-flex">
              <van-image
                class="u-m-r-10"
                :src="item.avatarfull"
                round
                height="38"
                width="38"
                fit="cover"
              ></van-image>
              <div class="u-flex-col u-col-top">
                <span class="u-line-1">{{ item.personaname }}</span>
                <span class="u-line-1" v-if="item.last_match_time">{{
                  formatTime(item.last_match_time)
                }}</span>
              </div>
            </div>
          </template>
        </van-cell>
      </template>
      <van-empty
        class="custom-image"
        image="https://iconfont.alicdn.com/t/24a09c69-d02d-4f9f-82dc-cb643ae42bac.png"
        image-size="200"
        description="暂无搜索内容"
      >
      </van-empty>
    </template>
  </div>
</template>
<script setup>
import { getSearchResult } from "@/api/search";
import { computed, ref, watch } from "vue";
import { useRouter } from "vue-router";
import { useStore } from "vuex";
const router = useRouter();
const store = useStore();
const keyword = ref("");
const historyList = computed(() => store.getters.historyList);
const searchList = ref([]);
const searchStatus = ref("init");
const formatTime = (startTime) => {
  const timeDiff = new Date().getTime() - new Date(startTime).getTime();
  const sec = Math.floor(timeDiff / 1000);
  const min = Math.floor(timeDiff / (60 * 1000));
  const hour = Math.floor(timeDiff / (60 * 60 * 1000));
  const day = Math.floor(timeDiff / (24 * 60 * 60 * 1000));
  const mounth = Math.floor(timeDiff / (30 * 24 * 60 * 60 * 1000));
  const year = Math.floor(timeDiff / (12 * 30 * 24 * 60 * 60 * 1000));
  if (sec < 60) {
    return "刚刚";
  } else if (min >= 1 && min < 60) {
    return `${min}分钟前`;
  } else if (hour >= 1 && hour < 24) {
    return `${hour}小时前`;
  } else if (day >= 1 && day <= 30) {
    return `${day}天前`;
  } else if (mounth >= 1 && mounth < 12) {
    return `${mounth}个月前`;
  } else {
    return `${year}年前`;
  }
};
watch(keyword, (val) => {
  if (!val) {
    searchStatus.value = "init";
    searchList.value = [];
  }
});
const handleToSearch = () => {
  if (!keyword.value) return;
  searchStatus.value = "search";
  store.dispatch("search/updateHistoryList", keyword.value);
  getSearchResult({ q: keyword.value })
    .then((res) => {
      searchList.value = res;
      searchStatus.value = "success";
    })
    .catch(() => {
      searchStatus.value = "error";
    });
};
const handleToSearchHistory = (historyKeyWord) => {
  keyword.value = historyKeyWord;
  handleToSearch();
};
const handleToBack = () => {
  router.back();
};
const handleToHome = (account_id) => {
  router.push(`/account/${account_id}`);
};
const handleToClearHistory = () => {
  store.dispatch("search/clearHistoryList");
};
</script>
<style lang="scss" scoped>
.search-box {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  background: rgb(16, 25, 37);
  z-index: 10;
}
.search-placeholder {
  height: 46px;
  width: 100vw;
}
.loading-box{
  width: 100%;
  padding: 100px 0;
  display: flex;
  align-items: center;
  justify-content: center;
}
</style>
